import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hospitable_rental/models/general_type.dart';
import 'package:hospitable_rental/utils/comme_picker/common_picker.dart';
import 'package:hospitable_rental/viewmodels/filter_bar_viewmodel.dart';
import 'package:hospitable_rental/widgets/filter_bar/data.dart';

import 'index_item.dart';
import 'package:provider/provider.dart';

class FilterBar extends StatefulWidget {
  final ValueChanged<FilterBarResult>? onChanged;

  const FilterBar({
    Key? key,
    this.onChanged,
  }) : super(key: key);

  @override
  _FilterBarState createState() => _FilterBarState();
}

class _FilterBarState extends State<FilterBar> {
  _loadData() {
    var result = Map<String, List<GeneralType>>();
    result["roomTypeList"] = roomTypeList;
    result["orientedList"] = orientedList;
    result["floorList"] = floorList;

    context.read<FilterBarViewModel>().dataList = result;
  }

  @override
  void initState() {
    Timer.run(_loadData);
    super.initState();
  }

  _onAreaChanged(BuildContext context) async {
    context.read<FilterBarViewModel>().isAreaActive = true;

    int value = areaList.indexWhere((element) => context.read<FilterBarViewModel>().areaType.id == element.id);
    if (value == -1) value = 0;

    print(value);

    var result = await CommonPicker.showPicker(
      context: context,
      value: value,
      options: areaList.map((e) => e.name.toString()).toList(),
    ).whenComplete(() => setState(() => context.read<FilterBarViewModel>().isAreaActive = false));

    print("result = $result");

    if (result == null) return;

    context.read<FilterBarViewModel>().areaType = areaList[result];

    print("_areaId = ${areaList[result]}");

    _onChanged();
  }

  _onRentTypeChanged(BuildContext context) async {
    context.read<FilterBarViewModel>().isRentTypeActive = true;

    int value = rentTypeList.indexWhere((element) => context.read<FilterBarViewModel>().rentType.id == element.id);
    if (value == -1) value = 0;

    print(value);

    var result = await CommonPicker.showPicker(
      context: context,
      value: value,
      options: rentTypeList.map((e) => e.name.toString()).toList(),
    ).whenComplete(() => setState(() => context.read<FilterBarViewModel>().isRentTypeActive = false));

    print("result = $result");

    if (result == null) return;

    context.read<FilterBarViewModel>().rentType = rentTypeList[result];

    print("_rentType = ${rentTypeList[result]}");

    _onChanged();
  }

  _onPriceChanged(BuildContext context) async {
    context.read<FilterBarViewModel>().isPriceActive = true;

    int value = priceList.indexWhere((element) => context.read<FilterBarViewModel>().priceType.id == element.id);
    if (value == -1) value = 0;

    print(value);

    var result = await CommonPicker.showPicker(
      context: context,
      value: value,
      options: priceList.map((e) => e.name.toString()).toList(),
    ).whenComplete(() => setState(() => context.read<FilterBarViewModel>().isPriceActive = false));

    print("result = $result");

    if (result == null) return;

    context.read<FilterBarViewModel>().priceType = priceList[result];

    print("priceType = ${priceList[result]}");

    _onChanged();
  }

  _onFilterChanged(BuildContext context) async {
    Scaffold.of(context).openEndDrawer();
  }

  _onChanged() {
    if (widget.onChanged != null) {
      widget.onChanged!(context.read<FilterBarViewModel>().result);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 44.0,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(bottom: BorderSide(color: Colors.black12)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          FilterBarItem(
            title: context.read<FilterBarViewModel>().areaType.name.toString(),
            isActive: context.read<FilterBarViewModel>().isAreaActive,
            onTap: _onAreaChanged,
          ),
          FilterBarItem(
            title: context.read<FilterBarViewModel>().rentType.name.toString(),
            isActive: context.read<FilterBarViewModel>().isRentTypeActive,
            onTap: _onRentTypeChanged,
          ),
          FilterBarItem(
            title: context.read<FilterBarViewModel>().priceType.name.toString(),
            isActive: context.read<FilterBarViewModel>().isPriceActive,
            onTap: _onPriceChanged,
          ),
          FilterBarItem(
            title: "筛选",
            onTap: _onFilterChanged,
          ),
        ],
      ),
    );
  }
}
